package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.CouponAdapter;
import com.shaoshaohuo.app.entity.MyCouponEntity;

import java.util.List;

public class CanUseCouponActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView couponList;
    /**
     * tobar
     */
    private ImageView order_tobar_back;
    private TextView order_tobar_enter;
    private ImageView order_tobar_right;
    private LinearLayout order_layout_topbar_left;
    private CouponAdapter couponAdapter;
    private List<MyCouponEntity.DataBean> dataBeen;
    private RelativeLayout coupon_no_data_relative;
    private String totalmoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_can_use_coupon);
        totalmoney = getIntent().getExtras().getString("totalmoney");
        dataBeen = (List<MyCouponEntity.DataBean>) getIntent().getExtras().getSerializable("coupondata");
        Log.e("TAG", "onCreate: " + totalmoney + dataBeen.size());
        initView();
        initListener();
//        loadData();
        bindAdapter();
    }


    private void bindAdapter() {
        couponList.setAdapter(couponAdapter);
    }

    private void initView() {
        /**
         * tobar
         * */
        order_tobar_back = (ImageView) findViewById(R.id.imageview_topbar_left_backImage);
        order_tobar_back.setVisibility(View.VISIBLE);
        order_tobar_back.setImageResource(R.mipmap.return_key);
        order_tobar_enter = (TextView) findViewById(R.id.toolbar_topname);
        order_tobar_enter.setText("优惠券");
        order_tobar_right = (ImageView) findViewById(R.id.imageview_message);
        order_tobar_right.setVisibility(View.GONE);
        order_layout_topbar_left = (LinearLayout) findViewById(R.id.layout_topbar_left);
        order_layout_topbar_left.setVisibility(View.VISIBLE);

        couponList = (ListView) findViewById(R.id.list_coupon);
        couponAdapter = new CouponAdapter(getApplicationContext(), dataBeen, "1");
        coupon_no_data_relative = (RelativeLayout) findViewById(R.id.coupon_no_data_relative);
    }

    private void initListener() {
        order_tobar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent();
//                intent.putExtra("money", dataBeen.get(position).getCouponIf());
//                setResult(120, intent);
                finish();
            }
        });
        couponList.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
//        intent.putExtra("money", dataBeen.get(position).getCouponIf());
        Bundle bundle = new Bundle();
        bundle.putString("money", dataBeen.get(position).getCouponMoney());
        bundle.putInt("position", position);
        intent.putExtras(bundle);
        setResult(120, intent);
        finish();
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        Intent intent = new Intent();
//        intent.putExtra("money", "");
//        setResult(120, intent);
//        finish();
//        return true;
//    }

}